package com.example.activitysign.controller;

import com.example.activitysign.pages.AjaxResult;
import com.example.activitysign.pages.PageResult;
import com.example.activitysign.pages.PageValidate;
import com.example.activitysign.service.IActivityUserJoinService;
import com.example.activitysign.validate.ActivityUserJoinCreateValidate;
import com.example.activitysign.validate.ActivityUserJoinSearchValidate;
import com.example.activitysign.validate.ActivityUserJoinUpdateValidate;
import com.example.activitysign.vo.activity.ActivityUserJoinListedVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 用户活动报名管理
 */
@Slf4j
@RestController
@RequestMapping("api/activityUserJoin")
public class ActivityUserJoinController {

    @Resource
    private IActivityUserJoinService iActivityUserJoinService;

    @PostMapping("/list")
    public AjaxResult<PageResult<ActivityUserJoinListedVo>> list(@Validated @RequestBody ActivityUserJoinSearchValidate searchValidate) {

        PageValidate pageValidate = new PageValidate();
        pageValidate.setPageSize(searchValidate.getPageSize());
        pageValidate.setPageNo(searchValidate.getPageNo());
        PageResult<ActivityUserJoinListedVo> list = iActivityUserJoinService.list(pageValidate, searchValidate);
        return AjaxResult.success(list);
    }

    @GetMapping("/detail")
    public AjaxResult<ActivityUserJoinListedVo> detail(@Validated @RequestParam("id") Long id) {
        ActivityUserJoinListedVo detail = iActivityUserJoinService.detail(id);
        return AjaxResult.success(detail);
    }

    /**
     * 用户活动报名新增
     *
     * @param createValidate
     * @return
     */
    @PostMapping("/add")
    public AjaxResult<Object> add(@Validated @RequestBody ActivityUserJoinCreateValidate createValidate) {
        Boolean boole = iActivityUserJoinService.add(createValidate);
        if (boole) {
            return AjaxResult.success();
        } else {
            return AjaxResult.failed("报名人数已满");
        }

    }

    /**
     * 用户活动报名编辑
     *
     * @param updateValidate
     * @return
     */
    @PostMapping("/edit")
    public AjaxResult<Object> edit(@Validated @RequestBody ActivityUserJoinUpdateValidate updateValidate) {
        iActivityUserJoinService.edit(updateValidate);
        return AjaxResult.success();
    }


}
